#!/usr/bin/python3.5
# -*- coding utf-8

from app import login
from flask_login import login_user, logout_user
from app.model.models import User, Channel
from flask import redirect, url_for, request
from . import auth


@login.user_loader
def load_user(id):
    users = User.query().all()

    for user in users:
        print(user.user_name)

    return User.query.get(int(id))


# 登录
@auth.route("/login")
def login():
    print("hello login page")
    channel = Channel.query.get(2)
    print("渠道名称: ", channel.channel)
    user_list = User.query.filter_by(user_name='开机').all()
    for user in user_list:
        print(user.user_name)

    if len(user_list) > 0:
        login_user(user_list[0])

    return "hello login page!"


# 登出
@auth.route("/logout")
def logout():
    logout_user()
    return redirect(url_for("login"))